﻿using System;
using System.Data.Entity.ModelConfiguration;

namespace Zinnia.Lite.Core.Imp
{
    /// <summary>
    /// ORM Lite 映射机制
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <remarks>
    /// 该机制是基于ZinniaLite实体,如果要用更底层的，这个映射机制需要自己重新写主键映射逻辑
    /// </remarks>
    public abstract class ZinniaLiteMap<T> : EntityTypeConfiguration<T>, IZinniaLiteMap where T : ZinniaLite
    {
        public ZinniaLiteMap()
        {
            Init();
        }
        protected Type GenericType => typeof(T);

        public virtual void Init()
        {
            ToTable(TableName(), SchemaName());
        }

        public virtual string TableName()
        {
            return GenericType.FullName.Substring(GenericType.FullName.LastIndexOf('.') + 1);
        }

        public virtual string SchemaName()
        {
            return "dbo";
        }
    }
    /// <summary>
    /// ORM Lite 映射机制，扩展默认主键
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public abstract class ZinniaLiteIdentityMap<T> : ZinniaLiteMap<T> where T : ZinniaLiteIdentity
    {
        public override void Init()
        {
            base.Init();
            HasKey(p => p.Id);
        }
    }
}
